/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.adminlog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class UsAdmin_Log
{
	private StringBuilder	strContent		= null;
	private String			strFolder		= "";
	private String			strTFileName	= "";
	private File			fiFile			= null;

	public StringBuilder getStrContent()
	{
		return strContent;
	}

	public void setStrContent(StringBuilder Content)
	{
		this.strContent = Content;
	}

	public String getStrFolder()
	{
		return strFolder;
	}

	public void setStrFolder(String Folder)
	{
		this.strFolder = Folder;
	}

	public String getStrTFileName()
	{
		return strTFileName;
	}

	public void setStrTFileName(String TFileName)
	{
		this.strTFileName = TFileName;
	}

	public File getFiFile()
	{
		return fiFile;
	}

	public void setFiFile(File temfiFile)
	{
		this.fiFile = temfiFile;
	}

	/*
	 * @param strTemFolder：日志文件夹名称，strValue：日志内容 ,Data:特殊文件夹 0文件夹自动加data名称
	 * 
	 */
	public UsAdmin_Log(String strTemFolder, StringBuilder strValue, int Data)
	{
		if(Data == 0)
		{
			this.setStrFolder(strTemFolder + Com_Para.zxyPath + "data");
			this.setStrContent(strValue);
		}else if(Data == -1)
		{
			this.setStrFolder(strTemFolder);
			this.setStrContent(strValue);
		}
	}

	public UsAdmin_Log(String strTemFolder, StringBuilder strValue)
	{
		this.setStrFolder(strTemFolder + Com_Para.zxyPath + "log");
		this.setStrContent(strValue);
	}

	public UsAdmin_Log(String strTemFolder, StringBuilder strValue, String strFileName)
	{
		this.setStrFolder(strTemFolder + Com_Para.zxyPath + "log");
		this.setStrContent(strValue);
		this.setStrTFileName(strFileName);
	}

	public UsAdmin_Log(String strTemFolder, StringBuilder strValue, String strFileName,String strSubTemFolder)
	{
		this.setStrFolder(strTemFolder + Com_Para.zxyPath + "log"+ Com_Para.zxyPath + strSubTemFolder);
		this.setStrContent(strValue);
		this.setStrTFileName(strFileName);
	}
	
	public void WriteData(String delay_code) throws IOException
	{
		HadForder();
		SaveFileData(delay_code);
	}

	public void InitWriteData(String delay_code) throws IOException
	{
		HadForder();
		InitSaveFileData(delay_code);
	}

	// 写日志文件
	public void WriteLog() throws IOException
	{
		HadForder();
		SaveFileDay();
	}

	public void WriteLogAll(StringBuilder strSub) throws IOException
	{
		HadForder();
		SaveFileDayAll(strSub);
	}

	// 写日志文件
	public void WriteLogSub(String strSub) throws IOException
	{
		HadForder();
		SaveFileDay(strSub);
	}

	// 写日志文件
	public void WriteLogSub() throws IOException
	{
		HadForder();
		SaveFileDay("DEC_SEC_");
	}

	// 读取文本文件内容
	public StringBuilder ReadFile(String inputFileName)
	{
		StringBuilder strResult = new StringBuilder();
		fiFile = new File(inputFileName);
		FileInputStream fr = null;
		if(!fiFile.exists())
		{
			strResult.append("");
		}else
		{
			InputStreamReader is = null;
			BufferedReader br = null;
			try
			{
				fr = new FileInputStream(inputFileName);
				is = new InputStreamReader(fr,Com_Para.U_CODE);
				br = new BufferedReader(is);
				while(true)
				{
					String line = br.readLine();
					if(java.util.Objects.equals(line,null))
						break;
					strResult.append(line);
				}
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			finally
			{
				try
				{
					is.close();
					br.close();
					fr.close();
				}
				catch(Exception ex)
				{
				}
			}
		}
		return strResult;
	}

	public StringBuilder ReadData(String delay_code, String param_name) throws IOException
	{
		StringBuilder strResult = new StringBuilder();
		strTFileName = strFolder + Com_Para.zxyPath + delay_code + ".dat";
		fiFile = new File(strTFileName);
		FileInputStream fr = null;
		if(!fiFile.exists())
		{
			strResult.append(Com_Fun.get_ope_result(param_name,"0","数据查询中或数据结果未生成,请稍候在试"));
		}else
		{
			InputStreamReader is = null;
			BufferedReader br = null;
			try
			{
				fr = new FileInputStream(strTFileName);
				is = new InputStreamReader(fr,Com_Para.U_CODE);
				br = new BufferedReader(is);
				while(true)
				{
					String line = br.readLine();
					if(java.util.Objects.equals(line,null))
						break;
					strResult.append(line);
				}
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			finally
			{
				try
				{
					is.close();
					br.close();
					fr.close();
				}
				catch(Exception ex)
				{
				}
			}
		}
		return strResult;
	}

	private void InitSaveFileData(String delay_code) throws IOException
	{
		strTFileName = strFolder + Com_Para.zxyPath + delay_code + ".dat";
		fiFile = new File(strTFileName);
		Writer sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(strTFileName),Com_Para.U_CODE));
			sw.write(strContent.toString());
			sw.close();
		}else
		{
			sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(strTFileName),Com_Para.U_CODE));
			sw.write(strContent.toString());
			sw.close();
		}
	}

	private void SaveFileData(String delay_code) throws IOException
	{
		strTFileName = strFolder + Com_Para.zxyPath + delay_code + ".dat";
		fiFile = new File(strTFileName);
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strTFileName,true);
			sw.write(strContent.toString());
			sw.close();
		}else
		{
			sw = new FileWriter(strTFileName,true);
			sw.write(strContent.toString());
			sw.close();
		}
	}

	private void SaveFileDayAll(StringBuilder strTContent) throws IOException
	{
		String strLogName = "";
		if(this.getStrTFileName().equals(""))
			strLogName = strFolder + Com_Para.zxyPath + "zxyong_"
					+ Com_Fun.DateTimeStr("yyyyMMdd") + "_0.log";
		else
			strLogName = strFolder + Com_Para.zxyPath + this.getStrTFileName()
					+ ".log";
		fiFile = new File(strLogName);
		long lgSize = 0;
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strLogName,true);
			sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
					+ "*********\r\n");
			sw.write(strContent.toString());
			sw.close();
		}else
		{
			lgSize = fiFile.length();
			int i = 1;
			while(true)
			{
				if(lgSize > Long.valueOf(1 * 1024 * 1024))
				{
					if(this.getStrTFileName().equals(""))
						strLogName = strFolder + Com_Para.zxyPath + "zxyong_"
								+ Com_Fun.DateTimeStr("yyyyMMdd") + "_"
								+ String.valueOf(i) + ".log";
					else
						strLogName = strFolder + Com_Para.zxyPath
								+ this.getStrTFileName()
								+ Com_Fun.DateTimeStr("yyyyMMdd") + "_"
								+ String.valueOf(i) + ".log";
					fiFile = new File(strLogName);
					if(!fiFile.exists())
					{
						fiFile.createNewFile();
						sw = new FileWriter(strLogName,true);
						sw.write("*********"
								+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
								+ "*********\r\n");
						sw.write(strContent.toString());
						sw.close();
					}else
					{
						lgSize = fiFile.length();
					}
				}else
				{
					sw = new FileWriter(strLogName,true);
					sw.write("*********"
							+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
							+ "*********\r\n");
					sw.write(strContent.toString());
					sw.close();
					break;
				}
				i++;
			}
		}
	}

	// 按日保存日志文件
	private void SaveFileDay(String Sub) throws IOException
	{
		String strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Sub
				+ Com_Fun.DateTimeStr("yyyyMMdd") + "_0.log";
		fiFile = new File(strLogName);
		long lgSize = 0;
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strLogName,true);
			sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
					+ "*********\r\n");
			sw.write(strContent.toString());
			sw.close();
		}else
		{
			lgSize = fiFile.length();
			int i = 1;
			while(true)
			{
				if(lgSize > Long.valueOf(1 * 1024 * 1024))
				{
					strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Sub
							+ Com_Fun.DateTimeStr("yyyyMMdd") + "_"
							+ String.valueOf(i) + ".log";
					fiFile = new File(strLogName);
					if(!fiFile.exists())
					{
						fiFile.createNewFile();
						sw = new FileWriter(strLogName,true);
						sw.write("*********"
								+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
								+ "*********\r\n");
						sw.write(strContent.toString());
						sw.close();
						break;
					}else
					{
						lgSize = fiFile.length();
					}
				}else
				{
					sw = new FileWriter(strLogName,true);
					sw.write("*********"
							+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
							+ "*********\r\n");
					sw.write(strContent.toString());
					sw.close();
					break;
				}
				i++;
			}
		}
	}

	// 按日保存日志文件
	private void SaveFileDay() throws IOException
	{
		String strLogName = "";
		if(this.getStrTFileName().equals(""))
			strLogName = strFolder + Com_Para.zxyPath + "zxyong_"
					+ Com_Fun.DateTimeStr("yyyyMMdd") + "_0.log";
		else
			strLogName = strFolder + Com_Para.zxyPath + this.getStrTFileName()
					+ ".log";

		fiFile = new File(strLogName);
		long lgSize = 0;
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strLogName,true);
			sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
					+ "*********\r\n");
			sw.write(strContent + "\r\n");
			sw.close();
		}else
		{
			lgSize = fiFile.length();
			int i = 1;
			while(true)
			{
				if(lgSize > Long.valueOf(1 * 1024 * 1024))
				{
					if(this.getStrTFileName().equals(""))
						strLogName = strFolder + Com_Para.zxyPath + "zxyong_"
								+ Com_Fun.DateTimeStr("yyyyMMdd") + "_"
								+ String.valueOf(i) + ".log";
					else
						strLogName = strFolder + Com_Para.zxyPath
								+ this.getStrTFileName()
								+ Com_Fun.DateTimeStr("yyyyMMdd") + "_"
								+ String.valueOf(i) + ".log";
					fiFile = new File(strLogName);
					if(!fiFile.exists())
					{
						fiFile.createNewFile();
						sw = new FileWriter(strLogName,true);
						sw.write("*********"
								+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
								+ "*********\r\n");
						sw.write(strContent.toString());
						sw.close();
						break;
					}else
					{
						lgSize = fiFile.length();
					}
				}else
				{
					sw = new FileWriter(strLogName,true);
					sw.write("*********"
							+ Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")
							+ "*********\r\n");
					sw.write(strContent.toString());
					sw.close();
					break;
				}
				i++;
			}
		}
	}

	// 判断文件夹是否存在，不存在则添加文件夹
	private void HadForder()
	{
		File file = new File(this.getStrFolder());
		if(!file.exists() && !file.isDirectory())
		{
			file.mkdir();
		}
	}
}